package com.prd.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.api.RemoteUserService;
import com.prd.config.LoginInterceptor;
import com.prd.constant.CheckStatus;
import com.prd.dto.*;
import com.prd.mapper.EntMeetSummaryMapper;
import com.prd.model.*;
import com.prd.service.EntMeetService;
import com.prd.service.EntMeetSummaryBudgetService;
import com.prd.service.EntMeetSummaryService;
import com.prd.util.StringsUtils;
import com.prd.util.UserInfoUtil;
import com.prd.vo.ExecuteSummaryDetailsVo;
import com.prd.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class EntMeetSummaryServiceImpl extends ServiceImpl<EntMeetSummaryMapper, EntMeetSummary> implements EntMeetSummaryService {

    @Autowired
    private EntMeetSummaryMapper entMeetSummaryMapper;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private EntMeetSummaryBudgetService entMeetSummaryBudgetService;

    @Autowired
    private EntMeetService entMeetService;

    @Override
    public int insertInfo(EntMeetSummary entMeetSummary) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        entMeetSummary.setStatus(1);
        entMeetSummary.setPerform(1);
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entMeetSummary);
        return entMeetSummaryMapper.insert(entMeetSummary);
    }

    @Override
    public int updateInfo(EntMeetSummary entMeetSummary) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entMeetSummary);
        return entMeetSummaryMapper.updateById(entMeetSummary);
    }

    @Override
    public int endorse(Long id) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        EntMeetSummary entMeetSummary = entMeetSummaryMapper.selectById(id);
        if (entMeetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }

        EntMeet entMeet = entMeetService.getById(entMeetSummary.getMeetId());
        if (entMeet == null) {
            throw new RuntimeException("记录没有找到！");
        }

        List<String> listVotePersonnel = Arrays.asList(entMeet.getVotePersonnel().split(","));
        if (!listVotePersonnel.contains(entMeet.getVotePersonnel())) {
            throw new RuntimeException("你不是投票人员！");
        }
        List<String> listEndorse = Arrays.asList(entMeetSummary.getEndorse().split(","));
        List<String> listAgainst = Arrays.asList(entMeetSummary.getAgainst().split(","));
        List<String> listVote = Arrays.asList(entMeetSummary.getVote().split(","));
        List<String> list = new ArrayList<>();
        list.addAll(listEndorse);
        list.addAll(listAgainst);
        list.addAll(listVote);
        if (list.contains(id)) {
            throw new RuntimeException("你已操作了！");
        }

        if (StringUtils.isNotBlank(entMeetSummary.getEndorse())) {
            entMeetSummary.setEndorse(entMeetSummary.getEndorse() + "," + loginUser.getId());
        } else {
            entMeetSummary.setEndorse(loginUser.getId().toString());
        }
        return updateInfo(entMeetSummary);
    }

    @Override
    public int against(Long id) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        EntMeetSummary entMeetSummary = entMeetSummaryMapper.selectById(id);
        if (entMeetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        EntMeet entMeet = entMeetService.getById(entMeetSummary.getMeetId());
        if (entMeet == null) {
            throw new RuntimeException("记录没有找到！");
        }

        List<String> listVotePersonnel = Arrays.asList(entMeet.getVotePersonnel().split(","));
        if (!listVotePersonnel.contains(entMeet.getVotePersonnel())) {
            throw new RuntimeException("你不是投票人员！");
        }
        List<String> listEndorse = Arrays.asList(entMeetSummary.getEndorse().split(","));
        List<String> listAgainst = Arrays.asList(entMeetSummary.getAgainst().split(","));
        List<String> listVote = Arrays.asList(entMeetSummary.getVote().split(","));
        List<String> list = new ArrayList<>();
        list.addAll(listEndorse);
        list.addAll(listAgainst);
        list.addAll(listVote);
        if (list.contains(id)) {
            throw new RuntimeException("你已操作了！");
        }
        if (StringUtils.isNotBlank(entMeetSummary.getAgainst())) {
            entMeetSummary.setAgainst(entMeetSummary.getAgainst() + "," + loginUser.getId());
        } else {
            entMeetSummary.setAgainst(loginUser.getId().toString());
        }
        return updateInfo(entMeetSummary);
    }

    @Override
    public int vote(Long id) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        EntMeetSummary entMeetSummary = entMeetSummaryMapper.selectById(id);
        if (entMeetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        EntMeet entMeet = entMeetService.getById(entMeetSummary.getMeetId());
        if (entMeet == null) {
            throw new RuntimeException("记录没有找到！");
        }

        List<String> listVotePersonnel = Arrays.asList(entMeet.getVotePersonnel().split(","));
        if (!listVotePersonnel.contains(entMeet.getVotePersonnel())) {
            throw new RuntimeException("你不是投票人员！");
        }
        List<String> listEndorse = Arrays.asList(entMeetSummary.getEndorse().split(","));
        List<String> listAgainst = Arrays.asList(entMeetSummary.getAgainst().split(","));
        List<String> listVote = Arrays.asList(entMeetSummary.getVote().split(","));
        List<String> list = new ArrayList<>();
        list.addAll(listEndorse);
        list.addAll(listAgainst);
        list.addAll(listVote);
        if (list.contains(id)) {
            throw new RuntimeException("你已操作了！");
        }
        if (StringUtils.isNotBlank(entMeetSummary.getVote())) {
            entMeetSummary.setVote(entMeetSummary.getVote() + "," + loginUser.getId());
        } else {
            entMeetSummary.setVote(loginUser.getId().toString());
        }
        return updateInfo(entMeetSummary);
    }

    @Override
    public int checkSummary(Long summaryId) {
        EntMeetSummary entMeetSummary = entMeetSummaryMapper.selectById(summaryId);
        if (entMeetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        entMeetSummary.setStatus(CheckStatus.TWO.getCode());
        return updateInfo(entMeetSummary);
    }

    @Override
    public int updateSummary(EntMeetSummary entMeetSummary) {
        EntMeetSummary meetSummary = entMeetSummaryMapper.selectById(entMeetSummary.getId());
        if (meetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        BeanUtils.copyProperties(entMeetSummary, meetSummary);
        return updateInfo(meetSummary);
    }

    @Override
    public int executeSummary(Long id) {
        EntMeetSummary meetSummary = entMeetSummaryMapper.selectById(id);
        if (meetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        meetSummary.setPerform(2);
        return updateInfo(meetSummary);
    }

    @Override
    public List<EntMeetSummary> listExecute(Long meetId, Integer options, Integer perform) {
        LambdaQueryWrapper<EntMeetSummary> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(meetId != null, EntMeetSummary::getMeetId, meetId);
        lambdaQueryWrapper.eq(options != null, EntMeetSummary::getOptions, options);
        lambdaQueryWrapper.eq(perform != null, EntMeetSummary::getPerform, perform);
        List<EntMeetSummary> list = entMeetSummaryMapper.selectList(lambdaQueryWrapper);
        if (list != null & list.size() > 0) {
            list.stream().forEach(item -> {
                List<Long> arrayList = new ArrayList<>();
                List<Long> executes = Arrays.asList(StringsUtils.convert(item.getExecute().split(",")));
                List<Long> supervisors = Arrays.asList(StringsUtils.convert(item.getSupervisor().split(",")));
                arrayList.addAll(executes);
                arrayList.addAll(supervisors);
                List<String> list1 = new ArrayList<>();
                List<String> list2 = new ArrayList<>();
                List<UserVo> listUser = remoteUserService.listInfo(arrayList.toArray(new Long[arrayList.size()])).getData();
                for (UserVo userVo : listUser) {
                    if (executes.contains(userVo.getId())) {
                        list1.add(userVo.getName());
                    }
                    if (supervisors.contains(userVo.getId())) {
                        list2.add(userVo.getName());
                    }
                }
                item.setExecute(String.join(" ", list1));
                item.setSupervisor(String.join(" ", list2));
            });
        }
        return list;
    }

    @Override
    public int advanceSummary(Long summaryId) {
        EntMeetSummary meetSummary = entMeetSummaryMapper.selectById(summaryId);
        if (meetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        meetSummary.setPerform(4);
        return updateInfo(meetSummary);
    }

    @Override
    public ExecuteSummaryDetailsVo listExecuteSummaryDetails(Long summaryId) {
        ExecuteSummaryDetailsVo detailsVo = new ExecuteSummaryDetailsVo();
        EntMeetSummary meetSummary = entMeetSummaryMapper.selectById(summaryId);
        if (meetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        List<Long> arrayList = new ArrayList<>();
        List<Long> executes = Arrays.asList(StringsUtils.convert(meetSummary.getExecute().split(",")));
        List<Long> supervisors = Arrays.asList(StringsUtils.convert(meetSummary.getSupervisor().split(",")));
        List<Long> tutors = Arrays.asList(StringsUtils.convert(meetSummary.getTutor().split(",")));
        arrayList.addAll(executes);
        arrayList.addAll(supervisors);
        arrayList.addAll(tutors);
        List<String> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        List<String> list3 = new ArrayList<>();
        List<UserVo> listUser = remoteUserService.listInfo(arrayList.toArray(new Long[arrayList.size()])).getData();
        for (UserVo userVo : listUser) {
            if (executes.contains(userVo.getId())) {
                list1.add(userVo.getName());
            }
            if (supervisors.contains(userVo.getId())) {
                list2.add(userVo.getName());
            }
            if (tutors.contains(userVo.getId())) {
                list3.add(userVo.getName());
            }
        }
        meetSummary.setExecute(String.join(" ", list1));
        meetSummary.setSupervisor(String.join(" ", list2));
        meetSummary.setTutor(String.join(" ", list3));
        detailsVo.setSummary(meetSummary);

        LambdaQueryWrapper<EntMeetSummaryBudget> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(EntMeetSummaryBudget::getSummaryId, summaryId);
        List<EntMeetSummaryBudget> budgetList = entMeetSummaryBudgetService.list(lambdaQueryWrapper);
        for (EntMeetSummaryBudget entMeetSummaryBudget : budgetList) {
            if (entMeetSummaryBudget.getType() == 1) {
                detailsVo.setBudget(entMeetSummaryBudget);
            }
            if (entMeetSummaryBudget.getType() == 2) {
                detailsVo.setActual(entMeetSummaryBudget);
            }
            if (entMeetSummaryBudget.getType() == 3) {
                detailsVo.setDifference(entMeetSummaryBudget);
            }
        }
        return detailsVo;
    }

    @Override
    public int agreeSummary(Long summaryId, Integer status, String instruct) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        EntMeetSummary meetSummary = entMeetSummaryMapper.selectById(summaryId);
        if (meetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        meetSummary.setStatus(status);
        meetSummary.setApprover(loginUser.getId().toString());
        meetSummary.setInstruct(instruct);
        return updateInfo(meetSummary);
    }

    @Override
    public JSONArray jurisdiction(Long summaryId) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        EntMeetSummary entMeetSummary = entMeetSummaryMapper.selectById(summaryId);
        if (entMeetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        List<Long> executes = Arrays.asList(StringsUtils.convert(entMeetSummary.getExecute().split(",")));
        List<Long> supervisors = Arrays.asList(StringsUtils.convert(entMeetSummary.getSupervisor().split(",")));
        List<Long> tutors = Arrays.asList(StringsUtils.convert(entMeetSummary.getTutor().split(",")));
        JSONArray jsonArray = new JSONArray();
        if (executes.contains(loginUser.getId())) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", "execute");
            jsonObject.put("name", "执行人");
            jsonArray.add(jsonObject);
        }
        if (supervisors.contains(loginUser.getId())) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", "supervisor");
            jsonObject.put("name", "督导人");
            jsonArray.add(jsonObject);
        }
        if (tutors.contains(loginUser.getId())) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", "tutor");
            jsonObject.put("name", "辅导人");
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    @Override
    public int addReplenish(ReplenishReportDto replenishReportDto) {
        EntMeetSummary entMeetSummary = entMeetSummaryMapper.selectById(replenishReportDto.getId());
        if (entMeetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        BeanUtils.copyProperties(replenishReportDto, entMeetSummary);
        entMeetSummary.setReplenishTime(new Date());
        return updateInfo(entMeetSummary);
    }

    @Override
    public int taskReject(Long summaryId, Integer examine) {
        EntMeetSummary entMeetSummary = entMeetSummaryMapper.selectById(summaryId);
        if (entMeetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        entMeetSummary.setExamine(examine);
        return updateInfo(entMeetSummary);
    }

    @Override
    public int addReport(ReportDto reportDto) {
        EntMeetSummary entMeetSummary = entMeetSummaryMapper.selectById(reportDto.getId());
        if (entMeetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        BeanUtils.copyProperties(reportDto, entMeetSummary);
        entMeetSummary.setReportTime(new Date());
        return updateInfo(entMeetSummary);
    }

    @Override
    public int addQualified(QualifiedDto qualifiedDto) {
        EntMeetSummary entMeetSummary = entMeetSummaryMapper.selectById(qualifiedDto.getId());
        if (entMeetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        BeanUtils.copyProperties(qualifiedDto, entMeetSummary);
        entMeetSummary.setQualifiedTime(new Date());
        return updateInfo(entMeetSummary);
    }

    @Override
    public int addFulfill(FulfillDto fulfillDto) {
        EntMeetSummary entMeetSummary = entMeetSummaryMapper.selectById(fulfillDto.getId());
        if (entMeetSummary == null) {
            throw new RuntimeException("记录没有找到！");
        }
        BeanUtils.copyProperties(fulfillDto, entMeetSummary);
        entMeetSummary.setFulfillTime(new Date());
        return updateInfo(entMeetSummary);
    }
}
